// import Vue from 'vue'
// import Vuex from 'vuex'
import {
    getToken,
    setToken,
    getCookie,
    removeToken
} from '@/utils/cookie'
import {
    getCaryardList,
    getCaryardBeingList,
    getCaryardByCaseNumber,
} from "./api/caryard.js"


Vue.use(Vuex)
const state = {
    userinfo: {
        username: null,
        login_last_time: null,
        login_list_ip: null,
    },
};

const mutations = {
    userinfo(state, userinfo) {
        state.userinfo.username = userinfo.username
        state.userinfo.login_last_time = userinfo.login_last_time
        state.userinfo.login_list_ip = userinfo.login_list_ip
    },
};

const actions = {
    login({
        commit
    }, data) {
        data.username = data.username.trim()
        return new Promise((resolve, reject) => {
            login(data).then(response => {
                setToken(response.data.token)
                resolve(response)
            }).catch(res => {
                reject(res)
            })
        })
    },
    getUserInfo({
        commit
    }) {
        return new Promise((resolve, reject) => {
            getUserInfo().then(response => {
                commit("userinfo", response.data)
                resolve(response)
            }).catch(res => {
                reject(res)
            })
        })
    },
    getLogList({
        commit
    }) {
        return new Promise((resolve, reject) => {
            getLogList().then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    getCaryardList({
        commit
    }) {
        return new Promise((resolve, reject) => {
            getCaryardList().then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    getCaryardBeingList({
        commit
    }) {
        return new Promise((resolve, reject) => {
            getCaryardBeingList().then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    putCaryard({
        commit
    }, data) {
        return new Promise((resolve, reject) => {
            putCaryard(data).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    postCaryard({
        commit
    }, data) {
        return new Promise((resolve, reject) => {
            postCaryard(data).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    deleteCaryard({
        commit
    }, data) {
        return new Promise((resolve, reject) => {
            deleteCaryard(data).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    getCaryardById({
        commit
    }, id) {
        return new Promise((resolve, reject) => {
            getCaryardById(id).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    getCaryardByCaseNumber({
        commit
    }, case_number) {
        return new Promise((resolve, reject) => {
            getCaryardByCaseNumber(case_number).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
};
export default new Vuex.Store({
    state,
    mutations,
    actions,
})